#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

typedef struct{
    char code[6];
    char name[20];
    char description[40];
    int  semesters;
    int  status;
}structCourse;

FILE *fp;

int insertCourseFile(structCourse tmpCourse){
    fp = fopen("myCourse", "ab");
    if(!fp){
        printf("O arquivo nao pode ser aberto. Encerrando o programa...\n");
        system("pause");
        exit(0);
    }

    if(fwrite(tmpCourse, sizeof(structCourse), 1, fp) == 1)
        printf("Curso salvo com sucesso!\n");
    else
        printf("Erro ao salvar o curso!\n");

    system("pause");
    fclose(fp);
}

int updateCourseFile(char keyCode[], char keyName[], structCourse tmpCourse){
    
}

int deleteCourseFile(char keyCode[], char keyName[]){
    
}

//RETORNA UMA STRUCT COM OS DADOS RESULTANTES DA BUSCA
structCourse findCourseFile(char keyCode[], char keyName[]){
    structCourse tmpCourse;

    fp = fopen("myCourse", "r+b");
    if(!fp){
        printf("O arquivo nao pode ser aberto. Encerrando o programa...\n");
        system("pause");
        exit(0);
    }

    rewind(fp);
    
    while (!feof(fp)) {
        fread(&tmpCourse, sizeof(structCourse), 1, fp);
        if(!strcmp(keyName, ""))//BUSCA APENAS POR CODIGO
            if(!strcmp(tmpCourse.code, keyCode)){
                fclose(fp);
                return tmpCourse;
            }
        else if(!strcmp(keyCode, "")) //BUSCA APENAS POR NOME
            if(!strcmp(tmpCourse.name, keyName)){
                fclose(fp);
                return tmpCourse;
            }
        else //BUSCA POR NOME E CODIGO
            if(!strcmp(tmpCourse.code, keyCode) && !strcmp(tmpCourse.name, keyName)){
            fclose(fp);
            return tmpCourse;
        }
    }
}

//PRINTA NO CONSOLE OS DADOS DO CURSO PASSADO COMO PARAMETRO
printCourseFile(structCourse *course){
    structCourse tmpCourse = *course;
    
    printf("\nCODIGO: %s\n", tmpCourse.code);
    printf("NOME: %s\n", tmpCourse.name);
    printf("DESCRICAO: %s\n", tmpCourse.description);
    printf("SEMESTRES: %d\n", tmpCourse.semesters);
    printf("STATUS: %d\n\n", tmpCourse.status);
    system("pause");
}

//LISTA OS CURSOS CADASTRADOS NO SISTEMA
listCourseFile(char keyName[]){
    structCourse tmpCourse;

    fp = fopen("myCourse", "r+b");
    if(!fp){
        printf("O arquivo nao pode ser aberto. Encerrando o programa...\n");
        system("pause");
        exit(0);
    }

    rewind(fp);
    printf("\n\nCURSOS CADASTRADOS\n\n");
    printf("%-6s %-20s\n", "******", "*******************");
    printf("%-6s %-20s\n", "CODIGO", "NOME");
    printf("%-6s %-20s\n", "******", "*******************");
    while (!feof(fp)) {
        if(fread(&tmpCourse, sizeof(structCourse), 1, fp))
            if(!strcmp(keyName, "")){
                if(strstr(tmpCourse.name, keyName) && tmpCourse.status == 0) //LISTA OS QUE POSSUEM PARTE DA KEUNAME INFOMRADA
                    printf("%-6s %-20s\n", tmpCourse.code, tmpCourse.name);
            }else if(tmpCourse.status == 0) //LISTA TODOS
                printf("%-6s %-20s\n", tmpCourse.code, tmpCourse.name);
    }

    fclose(fp);
}

//USAR NAS ENTRADAS DE DADOS - PASSA OS CARACTERES PARAR MAIÚSCULO
strToUpper(char keyName[]){
    int i;
    for (i = 0; keyName[i]; i++)
        keyName[i] = toupper(keyName[i]);

    return keyName;
}